package com.example.himalayaproject.find.anchor.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.anchor.adaptor.AnchorFragment_Adaptor;
import com.example.himalayaproject.find.anchor.bean.Entity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * A simple {@link Fragment} subclass.
 * 主播界面
 */
public class AnchorFragment extends Fragment implements Callback<Entity>,AnchorFragment_Adaptor.IsNextPageListener{
    private AnchorFragment_Adaptor adaptor;
    private int page=1;
    private  BaseApplication application;

    public AnchorFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View view=inflater.inflate(R.layout.fragment_anchor, container, false);
//
//
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        application= (BaseApplication) getActivity().getApplication();
        application.getHttpService().getList(page).enqueue(this);
        RecyclerView recyclerView= (RecyclerView) view.findViewById(R.id.AnchorFragment_recyclerView);
        adaptor=new AnchorFragment_Adaptor(this.getContext(),this);

        recyclerView.setAdapter(adaptor);
    }

    @Override
    public void onResponse(Call<Entity> call, Response<Entity> response) {
        adaptor.addAll(response.body().getList());
    }


    @Override
    public void onFailure(Call<Entity> call, Throwable t) {
        t.printStackTrace();
//        Toast.makeText(this.getContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void isNextPage() {
        page=page+1;
        if(page<=3) {
            application.getHttpService().getList(page).enqueue(this);
        }
    }



}
